package com.xgkj.diyiketang.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.DatiInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.DatiDetailTask;
import com.xgkj.diyiketang.task.PostMyAnswerTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.ClearableEditText;

public class TiankongtiActivity extends BaseActivity implements View.OnClickListener{
    private Context mContext;
    private RelativeLayout titleLayout;
    private TextView back;
    private RatingBar scoreLayout;
    private ImageView datiImage;
    private ClearableEditText yourAnswerEidt;
    private LinearLayout jiexiLayout;
    private ImageView jiexiImage;
    private ImageView duigouImage;
    private Button okBtn;
    private String myAnswer;
    private DatiInfo datiInfo;
    private String questionId;
    private int myScore;
    private int answerquestionId = -1;
    private int datiDetailTaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tiankongti);
        mContext = this;
        initView();
    }

    private void initView() {
        titleLayout = (RelativeLayout) findViewById(R.id.title_layout);
        back = (TextView) findViewById(R.id.back);
        scoreLayout = (RatingBar) findViewById(R.id.score_layout);
        datiImage = (ImageView) findViewById(R.id.dati_image);
        yourAnswerEidt = (ClearableEditText) findViewById(R.id.your_answer_eidt);
        okBtn = (Button) findViewById(R.id.ok_btn);
        okBtn.setOnClickListener(this);
        jiexiLayout = (LinearLayout) findViewById(R.id.jiexi_layout);
        jiexiImage = (ImageView) findViewById(R.id.jiexi_image);
        duigouImage = (ImageView) findViewById(R.id.duigou);
        duigouImage.setVisibility(View.GONE);

    }

    private void updateView(){
        scoreLayout.setNumStars(datiInfo.getRewardCount());
        ImageCache.getInstance(mContext).loadBitmapToView(datiImage,datiInfo.getQuestionImage(),null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDatiDetail();
    }

    private void getDatiDetail() {
        DatiDetailTask datiDetailTask = new DatiDetailTask(questionId);
        datiDetailTask.addListener(mListener);
        datiDetailTaskId = getEatShowApp().getTaskManager().addTask(datiDetailTask);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ok_btn:
                // TODO: 16/7/13 验证答案
                myAnswer = yourAnswerEidt.getText().toString();
                if (ViewUtil.isNull(myAnswer)){
                    ToastUtil.showMessage(mContext,R.string.please_check_your_answer);
                    return;
                }
                if (myAnswer.equalsIgnoreCase(datiInfo.getRightAnswer())){
                    myScore = datiInfo.getRewardCount();
                    duigouImage.setImageResource(R.drawable.duigou);
                    duigouImage.setVisibility(View.VISIBLE);
                }else {
                    myScore = -1;
                    duigouImage.setImageResource(R.drawable.duigou);
                    duigouImage.setVisibility(View.VISIBLE);

                }
                checkYourAnswer();
                break;
        }
    }

    private void checkYourAnswer(){
        postMyAnswer();
    }

    private void postMyAnswer(){
        PostMyAnswerTask task = new PostMyAnswerTask(questionId,myAnswer,myScore);
        task.addListener(mListener);
        answerquestionId = getEatShowApp().getTaskManager().addTask(task);

    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (answerquestionId == taskId){

            }else if (taskId == datiDetailTaskId){
                if (data != null){
                    datiInfo = (DatiInfo) data;
                    questionId = datiInfo.getQuestionId();
                    myAnswer = datiInfo.getUserAnswer();
                    myScore = datiInfo.getRewardCount();
                }
                updateView();
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == datiDetailTaskId ){
                finish();
            }
        }
    };
}
